﻿using System; using HISModels.ViewModel;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Permissions;
using System.Security.Principal;
using System.Web.Security;
using System.Threading;
using KeanuMvc.Reference;
using HIS.Utils.Securities;
using System.IO;
using HIS.Utils;
using HISModels.Entities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Configuration;

namespace HIS.Controllers.Physician {
    public class DoctorWorklist : HIS.Controllers.AppBaseController {
        #region Fields
        private string strAction = string.Empty;
        private string strNotification = string.Empty;
        private string DialogTitle = string.Empty;
        private string strJson = string.Empty;
        #endregion

        public DoctorWorklist() {
            try {
                Data.Add("docid", HIS.Utils.Helper.GetDoctorId());
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [Authorization(RoleAction = "physician-doctorworklist")]
        public override void Index() {
            try {
                Data.Add("Pagetitle", "WorkList Physician");
                RenderFile("Physician\\doctorworklist.html");
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [Authorization(RoleAction = "physician-doctorworklist")]
        public void TableWorkList() {
            try {
                //--------- Sample post to rest and get record as paging-------------/// 
                //Create paging object///
                string docid = HttpContext.Current.Request["docid"].NullSafeToString();
                string lobid = HttpContext.Current.Request["lobid"].NullSafeToString();
                string patientid = HttpContext.Current.Request["patientid"].NullSafeToString();
                string regtime = HttpContext.Current.Request["regtime"].NullSafeToString();
                string patientname = HttpContext.Current.Request["patientname"].NullSafeToString();
                string keywords = HttpContext.Current.Request["keywords"].NullSafeToString();
                string servicename = HttpContext.Current.Request["servicename"].NullSafeToString();
                string location = HttpContext.Current.Request["location"].NullSafeToString();

                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);
                pagingSearch.sSearch = "docid=" + docid + ";lobid=" + lobid + ";patientid=" + patientid + ";regtime=" + regtime + ";keywords=" + keywords + ";location=" + location;

                //Set Rest Url///
                string strUri = string.Empty;
                if (servicename.Equals("1"))
                    strUri = Global.ServiceUriDictionary["doctorworklistuntreated"] + Global.ServiceUriDictionary["requestformat"];
                else
                    strUri = Global.ServiceUriDictionary["doctorworklistweek"] + Global.ServiceUriDictionary["requestformat"];

                ///Post paging object to rest as json string///
                StringReader stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));
                ///Convert to JObject
                ///  
                JObject o = JObject.Parse(stringReader.ReadToEnd());
                JArray aaData = (JArray)o["aaData"];
                ///Modify it
                JArray aaDataModified = new JArray();
                for (int i = 0; i < aaData.Count(); i++) {
                    JObject oo = JObject.Parse(aaData[i].ToString());
                    oo["patientid"] = KeanuMvc.Reference.CryptoString.Encrypt(oo["patientid"].ToString());
                    aaDataModified.Add(oo);

                }
                o["aaData"] = aaDataModified;

                ///So serialize
                strJson = JsonConvert.SerializeObject(o);
                switch (lobid) {
                    case "1":
                        if (servicename == "1")
                            Data.Add("recordscount-etc-untreated", o["aaData"].Count());
                        else
                            Data.Add("recordscount-etc-last", o["aaData"].Count());
                        break;
                }

                HttpContext.Current.Response.Write(strJson);
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [Authorization(RoleAction = "physician-doctorworklist")]
        public void SendConsultation() {
            try {
                Data.Add("Pagetitle", "Send Consultation");
                RenderFile("Physician\\sendconsultation.html");
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [Authorization(RoleAction = "physician-doctorworklist")]
        public void ReplyConsultation() {
            try {
                Data.Add("Pagetitle", "Reply Consultation");
                RenderFile("Physician\\replyconsultation.html");
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [Authorization(RoleAction = "physician-doctorworklist")]
        public void ViewPatientData() {
            try {
                Data.Add("Pagetitle", "View Patient Data");
                RenderFile("Patient\\viewpatientdata.html");
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [Authorization(RoleAction = "physician-doctorworklist")]
        public string DropDownDoctor(string Selected) {
            StringReader stringReaderDropDown = null;
            string strResult = string.Empty;
            string strUri = Global.ServiceUriDictionary["hospitalroom"] + Global.ServiceUriDictionary["requestformat"];
            try {
                stringReaderDropDown = base.RestService.GetJson(strUri);

                List<HISModels.Entities.Doctor> listRole = JsonConvert.DeserializeObject<List<HISModels.Entities.Doctor>>(stringReaderDropDown.ReadToEnd());
                List<object[]> activeArr = new List<object[]>();

                foreach (HISModels.Entities.Doctor item in listRole) activeArr.Add(new object[] { item.Id, item.Employee.FirstName });

                strResult = KeanuMvc.Reference.Html.DropDown("ddhuroom", string.Empty, activeArr, Selected);
                activeArr.Clear();
                activeArr = null;
            } catch {
            } finally {
                stringReaderDropDown.Dispose();
            }
            return strResult;
        }
    }
}